<?php
require_once '../application/modules/loan/models/Loans.php';

class Clients extends HTech_Db_Table implements Emms_Interface_Geo, Emms_Interface_LoanApplication
{
    protected $_name    = 'view_clients';
    protected $_primary = 'ClientId';  
    
    protected $_dependentTables = array('Businesses','Loans');    

    protected $_referenceMap    = array(
        'Groups' => array(
            'columns'           => array('GroupId'),
            'refTableClass'     => 'Groups',
            'refColumns'        => array('GroupId')
        )
    );  

    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Clients");
        $this->_gridMetadata['titleMods'] = array('LocationShortName','OfficerUsername');
        $this->_gridMetadata['params'] = array('OfficerUserId','CommunityId','LocationId');
        $this->_gridMetadata['onClick'] = 'admin.client.view';
        $this->_gridMetadata['headers'] = array
            (
                array('ClientId',          50, $this->translate("Id"), 'Number'),
                array('OfficerUsername',   85, $this->translate("Officer"), 'String'),
                array('LocationShortName', 70, $this->translate("Branch"), 'String'),
                array('Community',        150, $this->translate("Community"), 'String'),
                array('CommunityType',     80, $this->translate("Com. type"), 'String'),
                array('GroupName',        250, $this->translate("Group"), 'String'),
                array('Code',             100, $this->translate("Identity number"), 'String'),
                array('CodeType',         100, $this->translate("Id. type"), 'String'),
                array('Gender',            60, $this->translate("Gender"), 'String'),
                array('Birthdate',         80, $this->translate("Birthdate"),     'DateTime', 'Date'),
                array('FullName',         180, $this->translate("Name"), 'String'),
                array('NickName',          80, $this->translate("Nickname"), 'String'),
                array('Children',          80, $this->translate("Children"), 'String'),
                array('Dependents',        80, $this->translate("Dpndnts"), 'String'),
                array('HomePhone',        100, $this->translate("Home phone"), 'String'),
                array('MobilePhone',      100, $this->translate("Mobile phone"), 'String')
            );
        return $this;
    }
    
    protected function _setRecordDependentRows()
    {
        $this->_record->r = (object) array('businesses' => null, 'loans' => null);
        $this->_record->r->businesses = $this->_record->d->findDependentRowset('Businesses');   
        $this->_record->r->loans = $this->_record->d->findDependentRowset('Loans');      
    }
            
    public function getPoints($id)
    {  
        $communities = Zend_Registry::get('db')->fetchAll(sprintf('
            select concat(b.FullName, ". ", b.Community) title, c.Latitude lat, c.Longitude lng, "%s" category
            from view_clients b, communities c 
            where b.ClientId = %s and c.CommunityId = b.CommunityId', self::EMMS_COMMUNITY, $id));            
        $locations = Zend_Registry::get('db')->fetchAll(sprintf('
            select o.Location title, o.Latitude lat, o.Longitude lng, "%s" category
            from view_clients c
            left join view_officers o on o.OfficerId = c.OfficerUserId
            where c.ClientId = %s', self::EMMS_LOCATION, $id));
        return array_merge($communities, $locations);
    }
    
    static function getActiveLoansCount($id)
    {
        return Zend_Registry::get('db')->fetchRow("select count(LoanId) hits from view_active_loans where ClientId = ?",$id)->hits;
    }
                
    static function getClientOptions($type = '', $id = 0)
    {
        if(!$id) return array();
        switch($type) {
            case 'Bank':
                return Zend_Registry::get('db')->fetchPairs("select ClientId, concat('G',right(GroupName,locate(' ', reverse(GroupName))-1), ': ', FullName) FullName 
                    from view_clients where BankId = ? order by FullName", $id);
            case 'Group':
                return Zend_Registry::get('db')->fetchPairs("select ClientId, FullName 
                    from view_clients where GroupId = ? order by FullName", $id);
        }
        return array();
    }
    
    public function getLoanApplicationData($id) 
    {
        $record = $this->getRecord($id);
        return (object) array
        (
            'BorrowerType' => 'I',
            'Name' => $record->d->FullName,
            'Code' => $record->d->ClientId,
            'Location' => $record->d->Location,
            'LocationId' => $record->d->LocationId,
            'Community' => $record->d->Community,
            'Officer' => $record->d->OfficerFullName,
            'OfficerUserId' => $record->d->OfficerUserId,
            'Clients' => (object) array($record->d)
        );
    }
}